<!--
 * @Author: hucc
 * @Date: 2021-10-18 19:45:30
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-18 20:05:19
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 1px solid green;
        }
    </style>
</head>

<body>
    用户名：<input type="text">
    <br>
    <div class='output'></div>
    <button>修改数据</button>
    <script>
        //获取dom节点
        let input = document.querySelector('input');
        let output = document.querySelector('.output');
        let button = document.querySelector('button');
        //声明数据模型
        let data = {
            username: null
        };
        //监听 
        //第一条线 当页面数据变化 模型数据跟着变化
        input.addEventListener('input', function() {
                output.innerText = this.value;
                //data.username = this.value;
            }, false)
            //第二条线 数据模型发生变化 页面的数据也发生变化
        button.addEventListener('click', function() {
            data.username = 'tom'
        }, false)
        Object.defineProperty(data, 'username', {
            set(value) {
                input.value = value;
                output.innerText = value;
            }
        })
    </script>
</body>

</html>